﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.IO.Compression;
using System.Diagnostics;

namespace Pwned
{
    class Connection
    {
        Thread listen;
        NetworkStream destinationStream;
        NetworkStream clientStream;

        int bytesReadServer = 0;
        byte[] fromServerBytes = new byte[1024];

        int bytesReadClient = 0;
        byte[] fromClientBytes = new byte[1024];

        const int OneSecond = 1000;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientStream">Local Client that connected</param>
        /// <param name="destinationStream">Destination to tunnel data to and from</param>
        public Connection(NetworkStream clientStream, NetworkStream destinationStream)
        {
            this.destinationStream = destinationStream;
            this.clientStream = clientStream;
        }

        public void Run()
        {
            listen = new Thread(new ThreadStart(Listen));

            listen.IsBackground = true;

            listen.Start();

            try
            {
                bytesReadServer = destinationStream.Read(fromServerBytes, 0, fromServerBytes.Length);

                int Transmitted = 0;
                Stopwatch Watch = new Stopwatch();
                
                while (bytesReadServer > 0)
                {
                    NetFlux.Statistics.BytesReceived += bytesReadServer;
                    Transmitted += bytesReadServer;

                    byte[] dec = fromServerBytes.Clone() as byte[];

                    clientStream.Write(dec, 0, bytesReadServer);

                    bytesReadServer = destinationStream.Read(fromServerBytes, 0, fromServerBytes.Length);
                }
                clientStream.Flush();
            }
            catch
            {
            }
            finally
            {
                destinationStream.Close();
                clientStream.Close();
                listen.Abort();
                Console.WriteLine("Closing Connection");
            }

        }

        public void Listen()
        {
            try
            {
                bytesReadClient = clientStream.Read(fromClientBytes, 0, fromClientBytes.Length);

                while (bytesReadClient > 0) 
                {
                    NetFlux.Statistics.BytesSent += bytesReadClient;

                    NetFlux.Statistics.BytesSent += bytesReadClient;
                    byte[] encrypted = fromClientBytes;
                    destinationStream.Write(encrypted, 0, bytesReadClient);

                    destinationStream.Flush();

                    bytesReadClient = clientStream.Read(fromClientBytes, 0, fromClientBytes.Length);
              
                }
                destinationStream.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                listen.Abort();
            }
        }

    }
}
